﻿'use strict'
var chirperApp = {};

chirperApp.friendsArray = [
//{name: "Jim", location: "Somewhere", photo: "", firebaseUrl: "chirperjimm"},
{ name: "Huseyin", 
location: "New Jersey", 
photo: "https://scontent-b-lga.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/579637_602499703100790_1686694538_n.jpg?oh=b7532f1a27079e9c2803d2274b3e2cdb&oe=5505C9E6", 
firebaseUrl: "chirperhuseyin" },
{name: "Louis", 
    location: "Austin, Tx", 
    photo: "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRj2ygVkS3xfs-KAmkTDxpGdLrqfKN91jH3BxmggXOTDjXIwZPV", 
firebaseUrl: "chirperlouism"},
];
chirperApp.receivedTweet = [];
chirperApp.sentTweet = [];

chirperApp.profile = {
    name: "Coral",
    location: "North Carolina",
    photo: "http://worthopedia.s3.amazonaws.com/images/thumbnails2/1/0811/28/1_31ae1e89336ed83487ab5615f4c72aad.jpg",
    firebaseUrl: "chirpercoralm"
}

chirperApp.urlMaker = function (address) {
    var url = "https://" + address + ".firebaseio.com/";
    for (var i = 1; i < arguments.length; i++) {
        url += arguments[i] + "/";
    };
    return url + ".json";
    console.log(url + ".json");
}

chirperApp.firebaseRequest = function (verb, url, data, success, error) {
    var request = new XMLHttpRequest();
    request.open(verb, url);
    request.onload = function () {
        if (this.status >= "200" && this.status < "400") {
            if (success && typeof (success) == 'function') {
                var data = JSON.parse(this.response);
                success(data);
            }
        }
    }
    request.onerror = function () {
        if (error && typeof (error) == 'function') {
            error(this.status);
        }
    }
    if (verb == 'GET' || verb == 'DELETE') {
        request.send();
    } else {
        request.send(JSON.stringify(data));
    }

}

chirperApp.friendsTweets = function () {

    var displayTweet = function (d) {
        for (var i in d) {
            d[i].id = i;
            chirperApp.receivedTweet.push(d[i]);
        }
        var h = "<div>";
        for (var i = 0; i < chirperApp.receivedTweet.length; i++) {            
            var tweets = chirperApp.receivedTweet[i];
            var user = tweets.user; 
            var tweet = tweets.message;
            var timestamp = new Date();
            
            h += "<h3>" + user + "</h3>";
            h += "<h5>" + timestamp + "</h5>"
            h += "<p>" + tweet + "</p>";            
        }
        h += "</div>";
        document.getElementById('panel-timeline').innerHTML = h;
    };

    for (var i in chirperApp.friendsArray) {
        var to = chirperApp.friendsArray[i].firebaseUrl;
        chirperApp.firebaseRequest('GET', chirperApp.urlMaker(to, "tweets"), null, displayTweet);
    }
}();

chirperApp.myTweets = function () {

    var displayTweet = function (d) {
        for (var i in d) {
            d[i].id = i;
            chirperApp.sentTweet.push(d[i]);
        }
        var h = "<div>";
        for (var i = 0; i < chirperApp.sentTweet.length; i++) {
            var tweets = chirperApp.sentTweet[i];
            var tweet = tweets.message;
            var timestamp = new Date();
            
            h += "<h3>Me</h3>";
            h += "<h5>" + timestamp + "</h5>"
            h += "<p>" + tweet + "</p>";            
        }
        h += "</div>";
        document.getElementById('panel-myTweets').innerHTML = h;
    };
    var url = chirperApp.profile.firebaseUrl;
    chirperApp.firebaseRequest('GET', chirperApp.urlMaker(url, "tweets"), null, displayTweet);
}();

chirperApp.sendTweet = function () {
    var input = document.getElementById('sendI');
    var tweet = input.value;
    var user = chirperApp.profile.name;    
    var url = chirperApp.profile.firebaseUrl;

    var newTweet = {
        name: user,
        timestamp: new Date(),
        message: tweet
    };
 
        chirperApp.sentTweet.push(newTweet);

        input.value = ' ';
        document.getElementById("sendL").style.visibility = "hidden";
        document.getElementById("sendI").style.visibility = "hidden";
        document.getElementById("sendB").style.visibility = "hidden";
        document.getElementById("send1").style.visibility = "visible";

    chirperApp.firebaseRequest('POST', chirperApp.urlMaker(url, "tweets"), newTweet);
}

chirperApp.friendsList = function () {
    var h = "<p>Friends</p>";
    h = "<div>";
    //var displayFriends = function (d) {
    //    for (var i in d) {
    //        var dd = i;
    //        for (var j in dd) {
    //            chirperApp.friendsArray.push(dd[i]);
    //        }
    //    }
       for (var i in chirperApp.friendsArray) {
        var friends = chirperApp.friendsArray[i];
        var name = friends.name;      
        var location = friends.location;        
        var pic = friends.photo;        
       
        h += "<h3>" + name + "</h3>";
        h += "<p>" + location + "</p>";
        h += "<img src='" + pic + "' style='width:100px;height:100px'/>"       
    }

    h += "</div>";
    document.getElementById("panel-friends").innerHTML = h;

    //var url = chirperApp.profile.firebaseUrl;
    //chirperApp.firebaseRequest('GET', chirperApp.urlMaker(url, "friends"), null, displayFriends);
}();

chirperApp.profilePage = function () {
    var h = "<div>";
    h += "<h2>" + chirperApp.profile.name + "</h2>";
    h += "<p>" + chirperApp.profile.location + "</p>";
    h += "<img src='" + chirperApp.profile.photo + "' style='width:100px;height:100px' />";
    h += "</div><br/><br/>";
    h += "<div>";
    h += "<button id='send1' onclick='chirperApp.writeTweet()'>Send Tweet</button>";
    h += "<label id='sendL' style='visibility: hidden'>Message</label>";
    h += "<input type='text' id='sendI' style='visibility: hidden' />";
    h += "<button id='sendB' style='visibility: hidden' onclick='chirperApp.sendTweet()'>Send</button>";
    h += "</div><br/><br/>";
    //h += "<div>";
    //h += "<label>Friends URL</label>";
    //h += "<input type='text' id='friendUrl' />";
    //h += "<button onclick='chirperApp.friendRequest()'>Send Friend Request</button>";
    //h += "</div>";

    document.getElementById("panel-profile").innerHTML = h;

    //var url = chirperApp.profile.firebaseUrl;
    //var sendTo = chirperApp.urlMaker(url, "profile");

    //chirperApp.firebaseRequest("POST", sendTo, chirperApp.profile);
}();

chirperApp.writeTweet = function () {
    document.getElementById("sendL").style.visibility = "visible";
    document.getElementById("sendI").style.visibility = "visible";
    document.getElementById("sendB").style.visibility = "visible";
    document.getElementById("send1").style.visibility = "hidden";
}

chirperApp.friendRequest = function () {
    var to = chirperApp.profile.firebaseUrl;
    var addFriends = function (d) {
        for (var i in d) {
            d[i].id = i;
            chirperApp.friendsArray.push(d[i]);
        }
        chirperApp.firebaseRequest('POST', chirperApp.urlMaker(to, 'friends'), d);
    }
    var friendUrl = document.getElementById('friendUrl');
    var url = friendUrl.value;
    chirperApp.firebaseRequest('GET', chirperApp.urlMaker(url, "profile"), null, addFriends);
}

chirperApp.deleteTweet = function () {

}

//chirperApp.setInterval();
